// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      meum: wx.getMenuButtonBoundingClientRect(),
      channelList: [],
      isActive: 0,
      articleList: [],
      isLoading: false,
      isError: false
  },
  getInfo(e) {
      this.setData({
          isActive: e.currentTarget.dataset.id,
      })
      this.getArticle()
  },
  getArticle() {
      this.setData({
          isLoading: true
      })
      wx.showLoading({
          title: "加载中～～"
      })
      wx.request({
          url: 'http://toutiao.itheima.net/v1_0/articles',
          data: {
              channel_id: this.data.isActive || 0,
              with_top: 1,
              timestamp: Date.now()
          },
          method: 'GET',
          success: ({
              data: {
                  data: {
                      results
                  }
              }
          }) => {
              const {
                  articleList
              } = this.data
              this.setData({
                  articleList: [...articleList, ...results],
                  isError: false,
                  isLoading: false
              })
              wx.hideLoading()
          },
          fail: () => {
              this.setData({
                  isError: true
              })
          }
      })
  },
  errRequest() {
      this.getArticle()
  },
  seachFn() {
      wx.navigateTo({
          url: "/pages/seach/seach",
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.getChannel()
      this.getArticle()
  },
  getChannel() {
      wx.showLoading({
          title: '加载中~~',
      })
      wx.request({
          url: 'http://toutiao.itheima.net/v1_0/user/channels',
          method: 'GET',
          success: (result) => {
              this.setData({
                  channelList: result.data.data.channels
              })
          },
          fail: (err) => {},
          complete: (res) => {
              wx.hideLoading()
          },
      })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
      this.getArticle()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
      if (this.data.isLoading) return
      this.getArticle()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})